<?php 

# LemurCMS by Alec Gorge
#
#     This file contains the PHP functions used throught the CMS.
#
# updated: Jan. 12, 2009

//Initiate classes
$page = new init;
$theme = new themeManager($page->page_info['id']);
	$theme_url = THEME_URL.'themes/'.trim($page->page_info['settings']['Theme']['value']).'/';
	$base_url = THEME_URL;
$plugins = $page->loadPlugins();
foreach($plugins as $value) {
	eval('require("'.$value.'");');
}
plugin('after-class-init');
function isPlugin ($pluginName, $installed = false) {
	global $pluginData, $page;	
	if(array_key_exists($pluginName, $pluginData)) {
		return true;
	}
	elseif($installed == true && array_key_exists($page->make_clean_url($pluginName),$pluginData)) {
		return true;
	}
	elseif(array_key_exists($page->make_clean_url($pluginName),$pluginData)) {
		return false;
	}
	else {
		return false;
	}
}

//Use clean URLS
	/*if($page->getSetting('Clean Urls') == "Enabled") {
		$clean = new CleanURL;
		$clean->parseURL(); 
		$clean->setParts('title', 'page'); 
	}*/

//Get the requested page id
	global $pageid;
	if($_GET[CONTENT_SLUG] && is_numeric($_GET[CONTENT_SLUG])) {
		$page->page_info['id'] = $_GET[CONTENT_SLUG];
		$GLOBALS['pageid'] = $_GET[CONTENT_SLUG];
		$_GET['page'] = $_GET[CONTENT_SLUG];
		$page->displayPage($GLOBALS['pageid']);
	}
	elseif ($_GET['page'] == "search") {
		$_GET['q'] = sanitize_sql_string($_GET['q']);
		plugin('search');
	}
	elseif ($_GET['page'] == "login") {
		$page->pageContent($adminLoginForm);
		$page->page_info['content']['page_title'] = "Login";
	}
	elseif(SIMPLE_CLEAN_URLS) {
		$_GET['page'] = trim($_GET['page'], '/');
		$_GET['page'] = end(explode('/',$_GET['page']));
		if(empty($_GET['page']))
			$_GET['page'] = $page->page_info['settings']['Home Page']['value'];
		else
			$_GET['page'] = $theme->idLookup($_GET['page'], true, true, true);
		$page->page_info['id'] = $_GET['page'];
		$GLOBALS['pageid'] = $_GET['page'];
		$page->displayPage($GLOBALS['pageid']);
	}
	else {
		$page->page_info['id'] = $page->page_info['settings']['Home Page']['value'];
		$_GET['page'] = $page->page_info['settings']['Home Page']['value'];
		$GLOBALS['pageid'] = $page->page_info['settings']['Home Page']['value'];
		$page->displayPage($GLOBALS['pageid']);
	}
	
//Adds content to the page_info array for use later
	

//functions used in the templates
	function count_nav_items($nav = 'primary') {
		global $theme;
		return $theme->countNavItems($nav);
	}
	function navigation(
		$a = "\n<ul id='ul-navigation'>\n%s\n</ul>\n",
		$b = "\n<ul class='ul-sub-navigation'>\n%s\n</ul>\n",
		$c = "\n\t<li><a href='%2\$s'>%1\$s</a></li>",
		$d = "\n\t<li class='selected'><a href='%2\$s'>%1\$s</a></li>",
		$e = "\n\t<li><a href='%2\$s'>%1\$s</a>%3\$s</li>",
		$f = "\n\t<li class='selected'><a href='%2\$s'>%1\$s</a>%3\$s</li>",
		$g = false
	) {
		global $theme;
		$defaults = array(
			'a' => '\n<ul id=\"ul-navigation\">\n%s\n</ul>\n',
			'b' => '\n<ul class=\"ul-sub-navigation\">\n%s\n</ul>\n',
			'c' => '\n\t<li><a href=\"%2\$s\">%1\$s</a></li>',
			'd' => '\n\t<li class=\"selected\"><a href=\"%2\$s\">%1\$s</a></li>',
			'e' => '\n\t<li><a href=\"%2\$s\">%1\$s</a>%3\$s</li>',
			'f' => '\n\t<li class=\"selected\"><a href=\"%2\$s\">%1\$s</a>%3\$s</li>'
		);
		if(is_array($g)) {
			$x = 'a';
			for($i = 0;$i<6;$i++) {
				if(isset($g[$i]))
					eval('$'.$x.' = "'.$g[$i].'";');
				else
					eval('$'.$x.' = "'.$defaults[$x].'";');
				$x++;
			}
		}
		$return =  $theme->navigation($a,$b,$c,$d,$e,$f);
		return $return;
	}
	function secondary_navigation(
		$a = "\n<ul id='ul-secondary-navigation'>\n%s\n</ul>\n",
		$b = "\n<ul class='ul-sub-navigation'>\n%s\n</ul>\n",
		$c = "\n\t<li><a href='%2\$s'>%1\$s</a></li>",
		$d = "\n\t<li class='selected'><a href='%2\$s'>%1\$s</a></li>",
		$e = "\n\t<li><a href='%2\$s'>%1\$s</a>%3\$s</li>",
		$f = "\n\t<li class='selected'><a href='%2\$s'>%1\$s</a>%3\$s</li>",
		$g = false
	) {
		global $theme;
		$defaults = array(
			'a' => '\n<ul id=\"ul-secondary-navigation\">\n%s\n</ul>\n',
			'b' => '\n<ul class=\"ul-sub-navigation\">\n%s\n</ul>\n',
			'c' => '\n\t<li><a href=\"%2\$s\">%1\$s</a></li>',
			'd' => '\n\t<li class=\"selected\"><a href=\"%2\$s\">%1\$s</a></li>',
			'e' => '\n\t<li><a href=\"%2\$s\">%1\$s</a>%3\$s</li>',
			'f' => '\n\t<li class=\"selected\"><a href=\"%2\$s\">%1\$s</a>%3\$s</li>'
		);
		if(is_array($g)) {
			$x = 'a';
			for($i = 0;$i<6;$i++) {
				if(isset($g[$i]))
					eval('$'.$x.' = "'.$g[$i].'";');
				else
					eval('$'.$x.' = "'.$defaults[$x].'";');
				$x++;
			}
		}
		$return =&  $theme->secondary_navigation($a,$b,$c,$d,$e,$f);
		return $return;
	}
	function static_content($a, $b, $c = 'no') {
		global $theme;
		$return =&  $theme->static_content($a,$b,$c);
		return $return;
	}
	function page_varible($title, $default) {
		
	}
	function get_setting($a = 'Site Name') {
		global $page;
		return $page->getSetting($a);
	}
	function page_content () {
		global $page,$theme_url, $base_url;
		ob_start();
		eval("?>{$page->page_info['content']['page_content']}");
		$content = ob_get_contents();
		ob_end_clean();
		plugin('page-content', array(&$content));
		return $content;
	}
	function page_title () {
		global $page,$theme_url, $base_url;
		ob_start();
		eval("?>{$page->page_info['content']['page_title']}");
		$title = ob_get_contents();
		ob_end_clean();
		plugin('page-title', array(&$title));
		return $title;
	}
	function page_menu_title () {
		global $page;
		return $page->page_info['content']['page_menu_title'];
	}
	function sidebar($a,$b, $c = false, $d = false) {
		global $page;
		return $page->formatSidebar($a,$b,$c,$d);
	}
	function get_sub_pages($str = true, $a = "\n<ul class='subpagelist'>\n%s\n</ul>", $b = "\n\t<li><a href=\"%2\$s\">%1\$s</a></li>", $c = "\n\t<li>This page has no sub pages.</li>") {
		global $theme;
		if($str === true)
			$str = $GLOBALS['pageid'];
		$return =&  $theme->subPagesOf($str, $a, $b, $c);
		return $return;
	}
	function page_id() {
		return $_GET['page'];
	}
	function get_parent_items ($id = '') {
		global $theme;
		if(empty($id))
			$id = $_GET['page'];
		return (array) $theme->buildParentTree($id);
	}
	function link_to_home () {
		return THEME_URL;
	}
	function bread_crumbs ($glue, $reverse = false, $link = false, $id ='') {
		global $theme;
		if(empty($id))
			$id = $_GET['page'];
		$items = get_parent_items($id);
		array_pop($items);
		foreach($items as $page) {
			if($link == false)
				$newArr[] = $theme->idLookup($theme->idLookup($page, true, true));
			else
				$newArr[] = $theme->makeLink($theme->idLookup($page, true, true));
		}
		if($reverse == true)
			$newArr = array_reverse((array) $newArr);
		//echo $glue.implode($glue, $newArr).$glue;
		if(count($newArr) > 0)
			return $glue.implode($glue, $newArr).$glue;
		else
			return $glue;
	}
	
	
//Assign vars used in the templates
	$pageid = $pageid;

//Assign page loading time
	$page_generation_time = round(microtime(), 4) - PAGE_LOAD_START;
	//$db_query_count = $GLOBALS['MYSQL_NUM_QUERIES'];
	//$db_query_time = $GLOBALS['MYSQL_QUERY_TIME'];

//Display page
	include(BASE_URL.'themes/'.trim($page->page_info['settings']['Theme']['value']).'/index.tpl.php');
	$theme->cleanStaticContentTable();
//Pre-release version raw print out of global array
if($_GET['debug'] == 'mysql') {
	echo '<div class="debug" style="padding:17px;background:black;opacity:0.9;color:white;position:fixed;bottom:0;left:0;height:100px;overflow:auto;width:98%;font-family:Consolas;">'.$page->getMySQLlog().'</div>';
}
if($_GET['debug'] == 'log') {
	echo '<div class="debug" style="padding:17px;background:black;opacity:0.9;color:white;position:fixed;bottom:0;left:0;height:100px;overflow:auto;width:98%;font-family:Consolas;">'.$page->getLog().'</div>';
}
if($_GET['debug'] == 'pageinfo') {
	$page->dump($page->page_info);
}
	?>